package com.iretailer.report.web.business;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iretailer.report.base.BaseAction;
import com.iretailer.report.dto.ReActivity;
import com.iretailer.report.model.Activity;
import com.iretailer.report.model.User;
import com.iretailer.report.util.RMap;
import com.iretailer.report.util.TimeUtil;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xjw
 * @Date: 2019/1/24 14:44
 * @Description:
 */
@RestController
public class ActivityController extends BaseAction {


    private Map<String, Object> grid(User user) {
        List<Activity> allActivities;
        if (user.getRoleId().equals("-")) {
            //如果是 root 权限 返回全部
            allActivities = activityService.list(new QueryWrapper<Activity>().orderByAsc("StartTime"));
        } else {
            allActivities = activityService.selectActivityByUserId(user.getId());
        }
        return RMap.ok(allActivities);
    }

    private Map<String, Object> save(ReActivity re) {
        String[] sts = re.getStartDateTime().split(":");
        String[] ets = re.getEndDateTime().split(":");

        LocalDateTime startDateTime = LocalDateTime.of(TimeUtil.parse_LocalDate(re.getStartDate()), LocalTime.of(Integer.parseInt(sts[0]), Integer.valueOf(sts[1]), 0));
        LocalDateTime endDateTime = LocalDateTime.of(TimeUtil.parse_LocalDate(re.getEndDate()), LocalTime.of(Integer.parseInt(ets[0]), Integer.valueOf(ets[1]), 0));

        boolean result = activityService.saveOrUpdate(re.getActivity(startDateTime, endDateTime), re.getMallIds());
        if (result) {
            return RMap.ok();
        } else {
            return RMap.error();
        }
    }

    private Map<String, Object> del(String id) {
        boolean result = activityService.delete(id);
        if (result) {
            return RMap.ok();
        } else {
            return RMap.errorNotDeleted();
        }
    }
}
